package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.util.Log;
import android.content.Context;
import android.content.Intent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.fragment.device.UniversalDeviceFragment;

import java.net.URL;

public class DeviceCardConfigActivity  extends AppActivity {
    public static void start(Context context, String deviceSerialNo){
        Intent intent = new Intent(context, DeviceCardConfigActivity.class);
        intent.putExtra("deviceSerialNo", deviceSerialNo);
        context.startActivity(intent);
    }

    private WebView deviceWebview;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_card_config;
    }

    @Override
    protected void initView() {
        deviceWebview = findViewById(R.id.universal_webview);
        WebSettings webSettings = deviceWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
//        String url = "www.baidu.com";
        DeviceBean deviceBean = LLDeviceDBManager.getInstance(getContext()).getDeviceBySerialNo(getString("deviceSerialNo"));
        String deviceControl = "";
        if(deviceBean.getDeviceType().equals("universal_freshair")){
            deviceControl = "";
        }else if(deviceBean.getDeviceType().equals("universal_sequencer")){
            deviceControl = "sequenceCardConfig";
        }else if(deviceBean.getDeviceType().equals("universal_projector")){
            deviceControl = "projectorCardConfig";
        }else if(deviceBean.getDeviceType().equals("universal_player")){
            deviceControl = "playCardConfig";
        }else if(deviceBean.getDeviceType().equals("universal_pa")){
            deviceControl = "powerAmplifierCardConfig";
        }
        String url = BuildConfig.DEVICE_URL + deviceControl +"?token="+ AppApplication.getInstance().getLoginInfo().getToken() +"&houseId="+ AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo() + "&deviceSerialNo=" + getString("deviceSerialNo") + "&phone="+ AppApplication.getInstance().getUserPhone();
        Log.i("11111", url);
        deviceWebview.loadUrl(url);
        //处理各种通知请求和事件，如果不使用该句代码，将使用内置浏览器访问网页
        deviceWebview.setWebViewClient(new WebViewClient());
        deviceWebview.setEnabled(false);
    }

    @Override
    public void onBackPressed() {
        if (deviceWebview.canGoBack()) {
            deviceWebview.canGoBack();
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void initData() {

    }
}
